package com.my.chapter12;

public class practice12 {
    public static void main(String[] args) {
        Sequence sequence = new Sequence(10);
        for(int i=0;i<11;i++){
            try{
                sequence.add(i);
            }catch (ArrayIndexOutOfBoundsException e){
                System.out.println("Exception：ArrayIndexOutOfBoundsException");
            }
        }
        Selector selector = sequence.selector();
        while(!selector.end()){
            System.out.print(selector.current() + " ");
            selector.next();
        }
    }
}

interface Selector{
    boolean end();
    Object current();
    void next();
}

class Sequence{
    private Object[] items;
    private int next = 0;
    public Sequence(int size){
        items = new Object[size];
    }
    public void add(Object x){
        if(next<items.length){
            items[next++] = x;
        }else{
            throw new ArrayIndexOutOfBoundsException();
        }
    }
    private class SequenceSelector implements Selector {
        private int i = 0;

        @Override
        public boolean end() {
            return i == items.length;
        }

        @Override
        public Object current() {
            return items[i];
        }

        @Override
        public void next() {
            if (i < items.length) {
                i++;
            }
        }
    }

    public Selector selector(){
        return new SequenceSelector();
    }
}
